﻿using DaHuaMes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Threading;
using Timer = System.Windows.Forms.Timer;

namespace DahuaMes
{
    public partial class MesUi : UserControl
    {
        public static string lb_id = "";
        public static DateTime initDateTime = DateTime.Now;

        public MesUi()
        {
            InitializeComponent();
           
        }


        private void MesUi_Load(object sender, EventArgs e)
        {
            MesFun.Instance.Load();
            userBox.Text = MesFun.Instance.Mes_Data.user;
            snBox.Text = MesFun.Instance.Mes_Data.process;
            passBox.Text = MesFun.Instance.Mes_Data.password;
            txt_logUrl.Text = MesFun.Instance.Mes_Data.logUrl;
            txt_upUrl.Text= MesFun.Instance.Mes_Data.upUrl;
            txt_imageUrl.Text = MesFun.Instance.Mes_Data.imageUrl;
            comboBox1.Text = MesFun.Instance.Mes_Data.Sn;
            //timer2.Enabled = true;

            timer2.Interval = 1000;
            timer2.Start();

        }
        private void button1_Click(object sender, EventArgs e)
        {
            UserLoginClass userLogin = new UserLoginClass();
            userLogin.userCode = userBox.Text;
            userLogin.pass = passBox.Text;
            string token = "";
            string msg = "";
            bool loginTool = MesFun.Instance.Login(userLogin, ref token, ref msg);
            if (loginTool)
            {
                MessageBox.Show("登录成功：" + msg);
                MesFun.b_isLog = true;
                button1.BackColor = Color.Green;
                initDateTime = DateTime.Now;
            }
            else
            {
                MessageBox.Show("登录失败：" + msg);
                MesFun.b_isLog = false;
                button1.BackColor = Color.Red;
            };
        }

        private void button2_Click(object sender, EventArgs e)
        {
            WebSnClass checkSnClass = new WebSnClass();
            Condition condition = new Condition();
            condition.columnName = "wp_code";
            condition.value = snBox.Text;
            checkSnClass.condition.Add(condition);

            condition = new Condition();
            condition.columnName = "lb_id";
            condition.value = comboBox1.Text;//"1019008"

            checkSnClass.condition.Add(condition);
            string msg = "";
            bool checksnTool = MesFun.Instance.checkSn(checkSnClass, "", ref msg);
            if (checksnTool)
            {
                MessageBox.Show("进站成功：" + msg);
            }
            else
            {
                MessageBox.Show("进站失败：" + msg);
            };
        }

        private void button3_Click(object sender, EventArgs e)
        {
            WebSnClass exitStationClass = new WebSnClass();
            Condition condition = new Condition();
            condition.columnName = "SN";
            condition.value = comboBox1.Text;
            exitStationClass.condition.Add(condition);
            condition = new Condition();
            condition.columnName = "WP_CODE";
            condition.value = snBox.Text;
            exitStationClass.condition.Add(condition);
            condition = new Condition();
            condition.columnName = "AA_TEST_RESULT";
            condition.value = "0";
            exitStationClass.condition.Add(condition);

            FuncexitStation赋值(ref exitStationClass);
            // checkSnClass.resultCode=snBox.Text;
            string msg = "";
            // checkSnClass.
            bool checksnTool = MesFun.Instance.exitStation(exitStationClass, "", ref msg);
       
            if (checksnTool)
            {
                MessageBox.Show("出站成功：" + msg);
            }
            else
            {
                MessageBox.Show("出站失败：" + msg);
            };
        }

        private void button4_Click(object sender, EventArgs e)
        {
            WebSnClass getTicket = new WebSnClass();         
            FuncgetTicket赋值(ref getTicket);
            string msg = "";
            bool checksnTool = MesFun.Instance.getTicket(getTicket, "", ref msg);
            string json = msg.Replace("OK", "");
            comboBox1.Items.Clear();
            try
            {
                ResponseClass responseClass = JsonConvert.DeserializeObject<ResponseClass>(json);
                for (int i = 0; i < responseClass.resultData.data.Count; i++)
                {
                    comboBox1.Items.AddRange(new object[] {
                    responseClass.resultData.data[i].lb_id});
                }
            }
            catch(Exception)
            {

            };
            //richTextBox1.Text = msg;
            if (checksnTool)
            {
                MessageBox.Show("获取工单成功：" + msg);
            }
            else
            {
                MessageBox.Show("获取工单失败：" + msg);
            };
        }
        private void button5_Click(object sender, EventArgs e)
        {
            WebSnClass pcbaBing = new WebSnClass();
            FuncpcbaBing赋值(ref pcbaBing);
            string msg = "";
            bool PCBADingTool = MesFun.Instance.PCBADing(pcbaBing, "", ref msg);
            string json = msg.Replace("OK", "");

            if (PCBADingTool)
            {
                MessageBox.Show("绑定成功："/* + msg*/);
            }
            else
            {
                MessageBox.Show("绑定失败：" /*+ msg*/);
            };
        }

        public void FuncpcbaBing赋值(ref WebSnClass pcbaBingClass)
        {
            Condition condition = new Condition();
            condition.columnName = "sn";
            condition.value = "";
            pcbaBingClass.condition.Add(condition);

            condition = new Condition();
            condition.columnName = "pcba1";
            condition.value = "TEST0003";
            pcbaBingClass.condition.Add(condition);

            condition = new Condition();
            condition.columnName = "pcba2";
            condition.value = "";
            pcbaBingClass.condition.Add(condition);

            condition = new Condition();
            condition.columnName = "wp_code";
            condition.value = "";
            pcbaBingClass.condition.Add(condition);

        }
        public void FuncexitStation赋值(ref WebSnClass exitStationClass)
        {
            Condition condition = new Condition();
            condition.columnName = "VICE_VALUE";

            List<TestResult> list = new List<TestResult>();

            TestResult testResult = new TestResult()
            {
                Test_Item = "AA",
                Value = "3",
                Low_Limit = "2",
                Up_Limit = "4",
                Result = "OK"
            };
            list.Add(testResult);
            //string s = JsonConvert.SerializeObject(list);
            condition.value = list;//测试值上传

            exitStationClass.condition.Add(condition);
        }
       



        public void FuncgetTicket赋值(ref WebSnClass checkSnClass) {
            Condition condition = new Condition();
            condition.columnName = "mo_code";
            condition.value = "WYP-TEST";
            checkSnClass.condition.Add(condition);
            condition = new Condition();
            condition.columnName = "types";
            condition.value = "assy";
            checkSnClass.condition.Add(condition);
        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                if (userBox.Text == "" || passBox.Text == "" || snBox.Text == "")
                {
                    MessageBox.Show("信息不完善！请输入后确认！");
                    return;
                }
                if (MessageBox.Show("是否保存现有数据？", "询问", MessageBoxButtons.YesNo) != DialogResult.Yes)
                    return;
                MesFun.Instance.Mes_Data.user=userBox.Text;
                MesFun.Instance.Mes_Data.process=snBox.Text;
                MesFun.Instance.Mes_Data.password=passBox.Text;
                MesFun.Instance.Mes_Data.logUrl = txt_logUrl.Text;
                MesFun.Instance.Mes_Data.upUrl = txt_upUrl.Text;
                MesFun.Instance.Mes_Data.imageUrl=txt_imageUrl.Text;
                MesFun.Instance.Mes_Data.Sn=comboBox1.Text;
                MesFun.Instance.Sava_MesData();
                MessageBox.Show("保存成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            Thread.Sleep(100);
            DateTime newDateTime = DateTime.Now;
            if ((newDateTime - initDateTime).TotalMinutes > 100 & button1.BackColor == Color.Green)
            {
                initDateTime = DateTime.Now;
                UserLoginClass userLogin = new UserLoginClass();
                userLogin.userCode = userBox.Text;
                userLogin.pass = passBox.Text;
                string token = "";
                string msg = "";
                bool loginTool = MesFun.Instance.Login(userLogin, ref token, ref msg);
                if (loginTool)
                {
                    MesFun.b_isLog = true;
                    button1.BackColor = Color.Green;
                    initDateTime = DateTime.Now;
                }
                else
                {
                    MesFun.b_isLog = false;
                    button1.BackColor = Color.Red;
                };
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
        upImage upImage=    new upImage();
         upImage.ShowDialog();

        }
    }
}
